package math.probability;

import java.util.ArrayList;
import java.util.Random;

public class UniformDistribution extends ProbDistribution {
	private Double a;
	private Double b;
	
	public UniformDistribution(Double a, Double b) {
		this.a = a;
		this.b = b;
	}
	
	public UniformDistribution() {
		this.a = 0.0;
		this.b = 1.0;
	}
	
	
	public ArrayList<Double> getSample(int n) {
		Random r = new Random();
		ArrayList<Double> sample = new ArrayList<Double>();
		for (int i = 0; i < n; i++) {
			sample.add(a + (b-a)*r.nextDouble());
		}
		return sample;
	}
	
	public ArrayList<Double> getSample(int n, Random r) {
		ArrayList<Double> sample = new ArrayList<Double>();
		for (int i = 0; i < n; i++) {
			sample.add(a + (b-a)*r.nextDouble());
		}
		return sample;
	}
	
	public String toString() {
		return "R" + String.format("(%2.2f;%2.2f)", a,b);
		 
	}

	@Override
	public Double getNext() {
		Random r = new Random();
		return a + (b-a)*r.nextDouble();
	}
	
	public Double getNext(Random r) {
		return a + (b-a)*r.nextDouble();
	}
	
	public Double getDisp() {
		return Math.pow(b-a, 2.0)/12;
	}
}
